package tree.递归.剑指;

import po.TreeNode;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 剑指Offer平衡二叉树55.java
 * @Description TODO https://leetcode-cn.com/problems/ping-heng-er-cha-shu-lcof/
 * @createTime 2021年03月29日 20:44:00
 */
public class 剑指Offer平衡二叉树55 {

    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        return Math.abs(countDeep(root.left) - countDeep(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
    }

    private int countDeep(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return Math.max(countDeep(root.left), countDeep(root.right)) + 1;
    }
}
